package com.glsc.ngateway.oaflow.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.glsc.ngateway.common.api.oaflow.resp.InterfaceResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.other.SystemConfig;
import com.glsc.ngateway.common.base.repo.mysql.gateway.other.SystemConfigRepository;
import com.glsc.ngateway.oaflow.service.FlowService;
import com.glsc.ngateway.oaflow.service.RPAService;
import com.glsc.ngateway.oaflow.utils.DictConstant;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class RPAController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    FlowService flowService;

    @Autowired
    RPAService rpaService;

    @Value("${RPA.workflowId}")
    public String workflowId;

    @Resource
    private SystemConfigRepository systemConfigRepository;

    @RequestMapping(method = RequestMethod.GET, value = "getRequestDataListForRPA", produces = {"application/json"})
    public String getRequestDataListForRPA() {
        List<String> requestIdList = rpaService.getRequestList();
        SystemConfig config = systemConfigRepository.findByConfigCode(DictConstant.RPA_WORKFLOWID);
        if (config != null && StringUtils.isNotEmpty(config.getConfigValue())){
            workflowId = config.getConfigValue();
        }
        List<Map<String, String>> dataMap = requestIdList.stream()
                .map(requestId->{
                    List<Map<String, String>> flowInfo = null;
                    try {
                        flowInfo = flowService.getRequestInfoFlow(requestId, "");
                    } catch (Exception e) {
                        return new HashMap<String, String>(0);
                    }
                    return CollectionUtil.isNotEmpty(flowInfo) ? flowInfo.get(0) : new HashMap<String, String>(0);
                })
                .filter(CollectionUtil::isNotEmpty)
                .collect(Collectors.toList());
        return JSON.toJSONString(InterfaceResponse.returnSuccss(dataMap));
    }

    @RequestMapping(method = RequestMethod.POST, value = "updateStatusForRPA", produces = {"application/json"})
    public String updateStatusForRPA(@RequestBody String requestList) {
        if (StringUtils.isEmpty(requestList)) {
            return JSON.toJSONString(InterfaceResponse.returnError("参数错误，requestid=" + requestList));
        }
        List<String> list = Arrays.asList(requestList.split(","));
        rpaService.updateRequestStatus(list);
        return JSON.toJSONString(InterfaceResponse.returnSuccss("更新成功，requestid=" + requestList));
    }
}
